package controllers;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

import play.cache.Cache;
import services.AuthorityService;
import services.IndexService;
import services.OperatorService;
import services.RoleMenuService;
import services.RoleUserService;
import utils.BizConstants;
import utils.Node;
import vos.MenuSearchVo;
import vos.OperatorSearchVo;
import vos.ResultVo;
import vos.ReturnCode;
import vos.ReturnListVo;
import vos.SystemMenuView;
import models.Operator;
import models.Oplog;
import models.Role;
import models.SystemAuthority;
import models.SystemMenu;

public class RoleMenuController extends BaseController {
	/**
	 * 跳转到菜单角色分配页面
	 *
	 * @Title: roleUser 
	 * @return: void
	 */
	public static void roleMenu(){
		render();
	}
	/**
	 * 获取菜单列表
	 *
	 * @Title: getOperator 
	 * @return: void
	 */
	public static void getUseMenu(String roleid){
		List<String> list=RoleMenuService.getRoleMenu(roleid);
		List<Node> nodes=new ArrayList<Node>();
    	List<SystemMenu> lists=RoleMenuService.getUseMenu();
    	for(SystemMenu menu : lists){
    		List<Node> childNode=new ArrayList<Node>();
    		List<SystemMenu> children=RoleMenuService.getMenus(menu.menuid);
    		for(SystemMenu m : children){
    			List<Node> authorityNodes=new ArrayList<Node>();
    			List<SystemAuthority> authoritys=AuthorityService.getAuthorityByMenu(m.menuid);
    			for(SystemAuthority authority : authoritys){
    				Node n=new Node("B-"+authority.id,authority.btnname,"open",false,"M-"+m.menuid,null);
    				if(list.contains(authority.id)){
        				n.checked=true;
        			}
    				authorityNodes.add(n);
    			}
    			Node n=new Node("M-"+m.menuid,m.menuname,"open",false,"M-"+m.parentid,authorityNodes);
    			if(list.contains(m.menuid)){
    				n.checked=true;
    			}
    			childNode.add(n);
    		}
    		Node node=new Node("M-"+menu.menuid,menu.menuname,"open",false,"",childNode);
    		if(list.contains(menu.menuid)){
    			node.checked=true;
			}
    		nodes.add(node);
    	}
    	renderJSON(nodes);
	}
	/**
	 * 获取操作权限
	 *
	 * @Title: getAuto 
	 * @return: void
	 */
	public static void getAuto(String menuid,String roleid){
		List<String> list=RoleMenuService.getRoleMenu(roleid);
//		String[] ids=id.split(";");
//		List<String> listid=new ArrayList<String>();
//		Collections.addAll(listid, id);
		List<SystemAuthority> authoritys=AuthorityService.getAuthorityByMenu(menuid);
		List<Node> lists=new ArrayList<Node>();
		for(SystemAuthority ss : authoritys){
			Node node=new Node(ss.id,ss.btnname);
			if(list.contains(ss)){
				node.checked=true;
			}
			lists.add(node);
		}
		renderJSON(lists);
	}
	/**
	 * 根据角色获取角色菜单
	 *
	 * @Title: getRoleOperator 
	 * @param roleid
	 * @return: void
	 */
	public static void getRoleMenu(String roleid){
		if(StringUtils.isEmpty(roleid)){
			renderFailedJson(ReturnCode.ErrorCode,"请选择角色");
		}
		List<String> list=RoleMenuService.getRoleMenu(roleid);
		ResultVo vo=new ResultVo(list);
		renderJSON(vo);
	}
	/**
	 * 保存角色菜单数据
	 *
	 * @Title: saveRoleUser 
	 * @param roleid 角色id
	 * @param ids 菜单id数组
	 * @return: void
	 */
	public static void saveRoleMenu(String roleid,String ids){
		if(StringUtils.isEmpty(roleid)){
			renderFailedJson(ReturnCode.ErrorCode,"请选择角色");
		}
		Set<String> menuSet=new HashSet<String>();
		if(StringUtils.isNotEmpty(ids)){
			String[] menuids=ids.split(",");
			Collections.addAll(menuSet, menuids);
		}
		else{
			menuSet=null;
		}
		boolean isok=RoleMenuService.saveRoleMenu(roleid,menuSet);
		if(isok){
			Operator oper=getLoginUser();
			Oplog oplog=new Oplog(oper.operatorid,"为角色"+roleid+"添加功能："+ids,"角色用户管理");
			oplog.save(oplog);
			renderFailedJson(ReturnCode.SuccessCode,"保存成功");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode,"保存失败");
		}
	}
}
